package com.qire.manhua.dal.service;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.qire.antsbinder.dal.DataService;
import com.qire.antsbinder.dal.annotation.StrategyAnnotation;
import com.qire.common.SummerApp;
import com.qire.common.collect.ListUtil;
import com.qire.common.constant.Url;
import com.qire.common.dal.WarehouseFactory;
import com.qire.manhua.R;
import com.qire.manhua.dal.IAppConfigDAL;
import com.qire.manhua.dal.IComicDataDAL;
import com.qire.manhua.model.viewModel.impl.ClassificationViewModel;
import com.qire.manhua.model.vo.ClassifiedComicVO;
import com.qire.manhua.model.vo.ClassifiedTagVO;

import java.util.List;

public class ClassificationService extends DataService {

    private final IComicDataDAL comicDataDAL = WarehouseFactory.create(this, IComicDataDAL.class);
    private final IAppConfigDAL appConfigDAL = WarehouseFactory.create(this, IAppConfigDAL.class);

    private final ClassificationViewModel viewModel;

    public ClassificationService(ClassificationViewModel viewModel) {
        this.viewModel = viewModel;
    }

    /**
     * 加载漫画分类种类标签
     */
    public void loadAllCategoryTag() {
        // 初始化分类标签数据
        Gson gson = new Gson();
        String categoryTag = SummerApp.summerApp().getString(R.string.classified_tag);
        List<ClassifiedTagVO> categoryVOList = gson.fromJson(categoryTag, new TypeToken<List<ClassifiedTagVO>>() {}.getType());
        if (categoryVOList != null) {
            requestAllCategoryTagApiCallBack(categoryVOList, true);
        }

        appConfigDAL.fetchAllClassifiedTag();
    }
    @StrategyAnnotation(event = Url.AllClassifiedTag)
    public void requestAllCategoryTagApiCallBack(List<ClassifiedTagVO> list, boolean isCache) {
        viewModel.clearClassifiedTagList();
        for(ClassifiedTagVO vo : list) {
            viewModel.addClassifiedTagItem(String.valueOf(vo.id), vo.cate_name);
        }
        viewModel.refreshClassifiedTagAdapter();
    }


    public void fetchClassifiedComicList(String classifiedTagId, String feeType, String stateType, String sortType, int pageNo) {
        comicDataDAL.fetchClassifiedComicList(classifiedTagId, feeType, stateType, sortType, pageNo);
    }
    @StrategyAnnotation(event = Url.HomeClassificationComicList)
    public void requestListDataCallback(List<ClassifiedComicVO> dataVOList, boolean isCache){

        viewModel.refreshLayoutFinish(300);
        if (ListUtil.isEmpty(dataVOList)) {
            viewModel.noMoreDataWasShown();
            viewModel.refreshEmptyDataUI();
            viewModel.refreshClassifiedComicAdapter();
            return;
        }

        for(ClassifiedComicVO itemVO : dataVOList) {
            int     comicId         = itemVO.id;
            String comicTitle       = itemVO.book_name;
            String shortTitle       = itemVO.short_title;
            String comicAuthor      = "";
            String coverImage       = itemVO.book_cover;
            String coverFloatTips   = "";
            String updateInfo       = "";
            int     popularityValue = itemVO.popularity;
            viewModel.addClassifiedComicItem(comicId, comicTitle, shortTitle, comicAuthor, coverImage, coverFloatTips, updateInfo, popularityValue);
        }

        if (!isCache) {
            viewModel.nextPage();
        }

        viewModel.refreshClassifiedComicAdapter();
        viewModel.refreshEmptyDataUI();
    }


}
